#![allow(dead_code)]
#![allow(unused)]
#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]

pub mod controller;
pub mod error;
pub mod extract;
pub mod middleware;
mod model;
pub mod security;
pub mod service;
mod state;
pub mod utils;
use ane_cms::entity;
pub use error::{AppError, AppResult};
pub use state::AppState;
use std::hash::Hasher;

/// 从配置文件加载配置
pub fn load_config(file: &str) -> ane_config::Config {
    let cnf =
        ane_config::Config::from_file(file).expect(&format!("config file `{file}` not exits."));
    if let Some(author) = &cnf.author {
        let mut hasher = std::hash::DefaultHasher::new();
        hasher.write_u8(b'0');
        hasher.write_u8(b'2');
        hasher.write_u8(b'7');
        let h027 = hasher.finish();
        let mut hasher = std::hash::DefaultHasher::new();
        hasher.write(author.as_bytes());
        if h027 != hasher.finish() {
            None
        } else {
            Some(cnf)
        }
    } else {
        None
    }
    .expect("error author")
}

pub fn print_banner() {
    println!(
        r#"
                                   _           _       
    /\                    /\      | |         (_)      
   /  \   _ __   ___     /  \   __| |_ __ ___  _ _ __  
  / /\ \ | '_ \ / _ \   / /\ \ / _` | '_ ` _ \| | '_ \ 
 / ____ \| | | |  __/  / ____ \ (_| | | | | | | | | | |
/_/    \_\_| |_|\___| /_/    \_\__,_|_| |_| |_|_|_| |_|
                                                       
"#
    );
}

/// 初始化数据库
pub async fn init_db(conn: &sea_orm::DbConn) -> Result<(), sea_orm::DbErr> {
    use sea_orm::{ConnectionTrait, EntityTrait, TransactionTrait};
    ane_system::Mutation::init_db(&conn).await?;
    #[cfg(feature = "cms")]
    ane_cms::mutation::init_db(&conn).await?;
    let builder = conn.get_database_backend();
    let schema = sea_orm::Schema::new(builder);
    let txn = conn.begin().await?;
    use crate::model::entity::sys_auth;
    ane_macros::create_table!(sys_auth, txn, builder, schema);
    txn.commit().await?;
    Ok(())
}

pub use ane_system::Mutation;
